package com.aistar.service.job.impl;

import com.aistar.mapper.CompanyMapper;
import com.aistar.mapper.EmployeeBlacklistMapper;
import com.aistar.mapper.RecruitmentMapper;
import com.aistar.model.entity.EmployeeBlacklist;
import com.aistar.service.job.IEmployeeBlackListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class EmployeeBlackListServiceImpl implements IEmployeeBlackListService {
    @Autowired
    private EmployeeBlacklistMapper employeeBlacklistMapper;

    @Autowired
    private RecruitmentMapper recruitmentMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public int saveBlackList(EmployeeBlacklist employeeBlacklist, Integer id, Integer eid) {
        List<Integer> blackList = employeeBlacklistMapper.findByRecruitmentId(eid);
        if (!blackList.contains(id)) {
            return employeeBlacklistMapper.addBlackList(employeeBlacklist);
        } else {
            return 0;
        }

    }

    @Override
    public int searchCompany(Integer eid, Integer cid) {
        return employeeBlacklistMapper.findInfo(eid, cid);
    }

    @Override
    public int dropBlack(Integer eid, Integer cid) {
        return employeeBlacklistMapper.DelBlack(eid, cid);
    }

    @Override
    public boolean judgeBlacklist(Integer employeeId, Integer recruitmentId) {
        //判断redis中是否存在数据
        HashOperations vop = redisTemplate.opsForHash();
        String key = "black_"+employeeId+"_"+recruitmentId;
        if(redisTemplate.hasKey(key)){
            String employeeId1= (String) vop.get(key,"employeeId".toString());
            String companyId1= (String) vop.get(key,"companyId".toString());
            return true;
        }else{
//            //// 根据求职者id和职位id判断该求职者是否屏蔽该招聘者所在公司
//            // 首先根据职位id获取该职位的公司id
            Integer companyId = recruitmentMapper.findById(recruitmentId).getCompanyId();
            // 根据求职者id和公司id去表中插数据
            Integer count = employeeBlacklistMapper.findInfo(employeeId,companyId);
            if(count==0){
                return false;
            }
            return true;
        }
    }

    @Override
    public void saveToRedis(Integer employeeId, Integer recruitmentId) {
        HashOperations vop = redisTemplate.opsForHash();
        String key = "black_"+employeeId+"_"+recruitmentId;
        EmployeeBlacklist employeeBlacklist=new EmployeeBlacklist();
        employeeBlacklist.setCompanyId(companyMapper.findById(recruitmentId).getId());
        employeeBlacklist.setEmployeeId(employeeId);
        Map<String,String> maps = new HashMap<>();
        maps.put("employeeId",employeeBlacklist.getEmployeeId().toString());
        maps.put("companyId",employeeBlacklist.getCompanyId().toString());
        vop.putAll(key,maps);
        redisTemplate.expire(key,3,TimeUnit.HOURS);
    }

    @Override
    public void saveToMySql(Integer id, Integer eid) {
        EmployeeBlacklist employeeBlacklist = new EmployeeBlacklist();
        employeeBlacklist.setEmployeeId(eid);
        employeeBlacklist.setCompanyId(id);
        saveBlackList(employeeBlacklist,id,eid);
    }

    @Override
    public void delToRedis(Integer employeeId, Integer recruitmentId) {
        HashOperations vop = redisTemplate.opsForHash();
        String key = "black_"+employeeId+"_"+recruitmentId;
        redisTemplate.delete(key);
    }


}
